package pk.nak.responses;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class LoginResponse extends BaseResponse {
	private static final String SUCCESSFUL_MSG = "verified";
	private Boolean verified;
	private String userid;
	private String message;
	@Override
	public void onSuccess(String response) {
		try{
			this.response = response;
			DocumentBuilderFactory dbfLoginResponse = DocumentBuilderFactory.newInstance();
	        DocumentBuilder dbLoginResponse = dbfLoginResponse.newDocumentBuilder();
	        
	        Document dom = dbLoginResponse.parse(new InputSource(new StringReader(response)));
	        NodeList nl = dom.getElementsByTagName("result");
	        if ( nl != null && nl.getLength() > 0 ){
	            this.message = nl.item(0).getFirstChild().getNodeValue();
	            this.userid = ((Element)nl.item(0)).getAttribute("userid");
	            
	            if ( this.message.compareToIgnoreCase(SUCCESSFUL_MSG) == 0){
	            	this.verified = true;
	            }else{
	            	this.verified = false;
	            }
	        }
		}catch(Exception ex){
			
		}
	}
	/*
	public void onSuccess(String response) {
		try{
			this.response = response;
			DocumentBuilderFactory dbfLoginResponse = DocumentBuilderFactory.newInstance();
	        DocumentBuilder dbLoginResponse = dbfLoginResponse.newDocumentBuilder();
	        
	        Document dom = dbLoginResponse.parse(new InputSource(new StringReader(response)));
	        NodeList nl = dom.getElementsByTagName("message");
	        if ( nl != null && nl.getLength() > 0 ){
	            this.message = nl.item(0).getFirstChild().getNodeValue();
	            
	            if ( this.message.compareToIgnoreCase(SUCCESSFUL_MSG) == 0){
	            	this.verified = true;
	            }else{
	            	this.verified = false;
	            }
	        }
		}catch(Exception ex){
			
		}
	}
	*/
	
	public String getUserID() {
		return userid;
	}

	public Boolean getVerified() {
		return verified;
	}

	public String getMessage() {
		return message;
	}
}
